package com.tamartech.greendb.ui.dialogs;

import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.data.ElectoralRollEntry;


public class RollEntryPropertiesDialog extends AbstractPropertiesDialog
{
    protected ElectoralRollEntryDAO dao;
    
    public RollEntryPropertiesDialog(ElectoralRollEntryDAO dao)
    {
        this(null, dao);
    }

    public RollEntryPropertiesDialog(ElectoralRollEntry rollEntry, ElectoralRollEntryDAO dao)
    {
        super ("rollEntryProperties", (rollEntry == null));
        
        this.dao = dao;
        
        if (rollEntry == null)
            rollEntry = new ElectoralRollEntry();
        
        initialiseForm (new RollEntryForm(rollEntry));
    }

    ElectoralRollEntry getEditingRollEntry()
    {
        return (ElectoralRollEntry) form.getFormModel().getFormObject();
    }
    
    @Override
    protected Object[] getObjectKeyFields()
    {
        // return list of fields that will identify the rollEntry in title bar or messages
        return getEditingRollEntry().getKeyFields();
    }

    @Override
    protected void doSave() throws Exception
    {
        dao.save (getEditingRollEntry());
    }

    @Override
    protected void doUpdate() throws Exception
    {
        dao.update (getEditingRollEntry());
    }
}
